/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xdialer_impl.h
 *
 * Purpose: dialer implementation base API define
 *
 * Developer:
 *   wen.gu , 2023-10-25
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_COM_INTERNAL_XDIALER_IMPL_H__
#define __XBUS_COM_INTERNAL_XDIALER_IMPL_H__

#include <string>
#include <memory>
#include <functional>
#include <map>
#include <mutex>

#include "xbus/core/xtype.h"
#include "xbus/core/xresult.h"
#include "xbus/core/xthread.h"
#include "xbus/com/xtypes.h"
#include "xbus/com/xdialer.h"

#define XBUS_DIALER_CONNECTION_TRY_INTERVAL_MS (40)

namespace xbus::com::internal {

class XDialerImpl final{
public:
    using XResultCode = core::XResultCode;
    using ConnectionId = uint64_t;
    using DialerHandler = std::function<core::XResult<XComId>(const std::string& url, int32_t port)>;
    using OnConnectionHandler = std::function<void(XDialerSp)>;

    struct ConnectionInfo {
        bool running;
        int32_t port;
        std::string url;
        core::XThread::XThreadPtr timer_worker;
    };

    using ConnectionMap = std::map<ConnectionId, ConnectionInfo>;
public:
    ~XDialerImpl();
protected:
    XDialerImpl();

public:
    static XDialerImpl& getInstance();

public:
    core::XResult<XDialerSp> tryConnect(const std::string& url, int32_t port = 0, int32_t timeout_ms = core::WaitType::NoWait);
    core::XResult<ConnectionId> registerConnection(const std::string& url, int32_t port, OnConnectionHandler handler);
    XResultCode unregisterConnection(const ConnectionId& handle); 

public:
    //void registerDialer(XComType type, DialerHandler handler);
private:
    ConnectionMap connection_map_;
    std::mutex lock_;
};

} //namespace xbus::com::internal


#endif /** !__XBUS_COM_INTERNAL_XDIALER_IMPL_H__ */